<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-pint" tabindex="-1"><a class="header-anchor" href="#laravel-pint"><span>Laravel Pint</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#running-pint">运行 Pint</a></li>
<li><a href="#configuring-pint">配置 Pint</a>
<ul>
<li><a href="#presets">预设</a></li>
<li><a href="#rules">规则</a></li>
<li><a href="#excluding-files-or-folders">排除文件/文件夹</a></li>
</ul>
</li>
<li><a href="#continuous-integration">持续集成</a>
<ul>
<li><a href="#running-tests-on-github-actions">GitHub Actions</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/pint" target="_blank" rel="noopener noreferrer">Laravel Pint</a> 是面向极简主义者的 PHP 代码风格修复工具。Pint 建立在 PHP-CS-Fixer 之上，简化了确保代码风格保持清洁和一致的过程。</p>
<p>Pint 自动安装在所有新的 Laravel 应用程序中，因此您可以立即开始使用它。默认情况下，Pint 不需要任何配置，并且将通过遵循 Laravel 的主观编码风格来修复代码风格问题。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>Pint 已包含在 Laravel 框架的最新版本中，因此通常不需要安装。但是，对于旧应用程序，您可以通过 Composer 安装 Laravel Pint：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/pint <span class="token parameter variable">--dev</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="运行-pint" tabindex="-1"><a class="header-anchor" href="#运行-pint"><span>运行 Pint</span></a></h2>
<p>您可以通过调用项目的 <code v-pre>vendor/bin</code> 目录中的 <code v-pre>pint</code> 二进制文件来指示 Pint 修复代码风格问题：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您还可以在特定文件或目录上运行 Pint：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint app/Models</span>
<span class="line"></span>
<span class="line">./vendor/bin/pint app/Models/User.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Pint 将显示更新的所有文件的详细列表。您可以通过在调用 Pint 时提供 <code v-pre>-v</code> 选项来查看有关 Pint 更改的更多详细信息：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">-v</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您希望 Pint 仅检查代码样式错误而不实际更改文件，可以使用 <code v-pre>--test</code> 选项。如果发现任何代码风格错误，Pint 将返回非零退出代码：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">--test</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您希望 Pint 仅修改根据 Git 有未提交更改的文件，您可以使用 <code v-pre>--dirty</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">--dirty</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您希望 Pint 修复任何具有代码风格错误的文件，但同时在修复任何错误时退出并返回非零退出代码，您可以使用 <code v-pre>--repair</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">./vendor/bin/pint <span class="token parameter variable">--repair</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="配置-pint" tabindex="-1"><a class="header-anchor" href="#配置-pint"><span>配置 Pint</span></a></h2>
<p>如前所述，Pint 不需要任何配置。但是，如果您希望自定义预设、规则或检查的文件夹，您可以在项目的根目录中创建一个 <code v-pre>pint.json</code> 文件：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"preset"</span><span class="token operator">:</span> <span class="token string">"laravel"</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，如果您希望使用特定目录中的 <code v-pre>pint.json</code>，您可以在调用 Pint 时提供 <code v-pre>--config</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pint <span class="token parameter variable">--config</span> vendor/my-company/coding-style/pint.json</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="预设" tabindex="-1"><a class="header-anchor" href="#预设"><span>预设</span></a></h3>
<p>预设定义了一组规则，可用于修复代码风格问题。默认情况下，Pint 使用 <code v-pre>laravel</code> 预设，通过遵循 Laravel 的主观编码风格来修复问题。但是，您可以通过向 Pint 提供 <code v-pre>--preset</code> 选项来指定不同的预设：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">pint <span class="token parameter variable">--preset</span> psr12</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果需要，您也可以在项目的 <code v-pre>pint.json</code> 文件中设置预设：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"preset"</span><span class="token operator">:</span> <span class="token string">"psr12"</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Pint 目前支持的预设有：<code v-pre>laravel</code>、<code v-pre>per</code>、<code v-pre>psr12</code> 和 <code v-pre>symfony</code>。</p>
<h3 id="规则" tabindex="-1"><a class="header-anchor" href="#规则"><span>规则</span></a></h3>
<p>规则是 Pint 将用于修复代码风格问题的样式指南。如上所述，预设是预定义的规则组，应该适用于大多数 PHP 项目，因此您通常不需要担心它们包含的各个规则。</p>
<p>然而，如果你愿意的话，你可以在你的 <code v-pre>pint.json</code> 文件中启用或禁用特定规则：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"preset"</span><span class="token operator">:</span> <span class="token string">"laravel"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"rules"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"simplified_null_return"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"braces"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"new_with_braces"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token property">"anonymous_class"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token property">"named_class"</span><span class="token operator">:</span> <span class="token boolean">false</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Pint 是建立在 <a href="https://github.com/FriendsOfPHP/PHP-CS-Fixer" target="_blank" rel="noopener noreferrer">PHP-CS-Fixer</a> 之上的。因此，你可以使用它的任何规则来修复项目中的代码风格问题：<a href="https://mlocati.github.io/php-cs-fixer-configurator" target="_blank" rel="noopener noreferrer">PHP-CS-Fixer Configurator</a>。</p>
<h3 id="排除文件-文件夹" tabindex="-1"><a class="header-anchor" href="#排除文件-文件夹"><span>排除文件/文件夹</span></a></h3>
<p>默认情况下，Pint 将检查项目中除了 <code v-pre>vendor</code> 目录中的所有 <code v-pre>.php</code> 文件。如果你希望排除更多文件夹，你可以使用 <code v-pre>exclude</code> 配置选项：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"exclude"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string">"my-specific/folder"</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你希望排除所有包含特定名称模式的文件，你可以使用 <code v-pre>notName</code> 配置选项：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"notName"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string">"*-my-file.php"</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你希望通过提供文件的确切路径来排除文件，你可以使用 <code v-pre>notPath</code> 配置选项：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"notPath"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string">"path/to/excluded-file.php"</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="持续集成" tabindex="-1"><a class="header-anchor" href="#持续集成"><span>持续集成</span></a></h2>
<h3 id="github-actions" tabindex="-1"><a class="header-anchor" href="#github-actions"><span>GitHub Actions</span></a></h3>
<p>要使用 Laravel Pint 自动化检查你的项目代码风格，你可以配置 <a href="https://github.com/features/actions" target="_blank" rel="noopener noreferrer">GitHub Actions</a> 在代码推送到 GitHub 时运行 Pint。首先，请确保在 GitHub 的 <strong>Settings &gt; Actions &gt; General &gt; Workflow permissions</strong> 中为 workflows 授予“读取和写入权限”。然后，创建一个名为 <code v-pre>.github/workflows/lint.yml</code> 的文件，并包含以下内容：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">name: Fix Code Style</span>
<span class="line"></span>
<span class="line">on: [push]</span>
<span class="line"></span>
<span class="line">jobs:</span>
<span class="line">  lint:</span>
<span class="line">    runs-on: ubuntu-latest</span>
<span class="line">    strategy:</span>
<span class="line">      fail-fast: true</span>
<span class="line">      matrix:</span>
<span class="line">        php: [8.3]</span>
<span class="line"></span>
<span class="line">    steps:</span>
<span class="line">      - name: Checkout code</span>
<span class="line">        uses: actions/checkout@v4</span>
<span class="line"></span>
<span class="line">      - name: Setup PHP</span>
<span class="line">        uses: shivammathur/setup-php@v2</span>
<span class="line">        with:</span>
<span class="line">          php-version: ${{ matrix.php }}</span>
<span class="line">          extensions: json, dom, curl, libxml, mbstring</span>
<span class="line">          coverage: none</span>
<span class="line"></span>
<span class="line">      - name: Install Pint</span>
<span class="line">        run: composer global require laravel/pint</span>
<span class="line"></span>
<span class="line">      - name: Run Pint</span>
<span class="line">        run: pint</span>
<span class="line"></span>
<span class="line">      - name: Commit linted files</span>
<span class="line">        uses: stefanzweifel/git-auto-commit-action@v5</span>
<span class="line">        with:</span>
<span class="line">          commit_message: "Fixes coding style"</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/pintmd/16726" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pi...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/pintmd/16726" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pi...</a></p>
</blockquote>
</div></template>


